package com.panshi.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.panshi.entity.AppPraiseTread;
import com.panshi.service.PraiseTreadService;
import com.panshi.tools.DataResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 点赞、踩 controller
 */
@RestController
@Api(tags = "点赞、踩业务接口")
public class PraiseTreadController {

    @Autowired
    private PraiseTreadService praiseTreadService;

    @ApiOperation("回答点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId", value="点赞用户id", required=true),
            @ApiImplicitParam(name="typeId", value="回答id", required=true)
    })
    @GetMapping("pt/a-estimate/{userId}/{typeId}")
    @HystrixCommand(fallbackMethod = "estimateGet")
    public DataResult estimateA(@PathVariable Integer userId,@PathVariable Integer typeId){
        AppPraiseTread praiseTread = new AppPraiseTread();
        praiseTread.setUserId(userId).setTypeId(typeId).setType("a").setPtStatus("1");
        return praiseTreadService.estimate(praiseTread);
    }

    @ApiOperation("回答点踩")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId", value="点赞用户id", required=true),
            @ApiImplicitParam(name="typeId", value="回答id", required=true)
    })
    @GetMapping("pt/at-estimate/{userId}/{typeId}")
    @HystrixCommand(fallbackMethod = "estimateGet")
    public DataResult estimateAt(@PathVariable Integer userId,@PathVariable Integer typeId){
        AppPraiseTread praiseTread = new AppPraiseTread();
        praiseTread.setUserId(userId).setTypeId(typeId).setType("a").setPtStatus("2");
        return praiseTreadService.estimate(praiseTread);
    }

    @ApiOperation("回答评论点赞")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId", value="点赞用户id", required=true),
            @ApiImplicitParam(name="typeId", value="回答评论id", required=true)
    })
    @GetMapping("pt/am-estimate/{userId}/{typeId}")
    @HystrixCommand(fallbackMethod = "estimateGet")
    public DataResult estimateAm(@PathVariable Integer userId,@PathVariable Integer typeId){
        AppPraiseTread praiseTread = new AppPraiseTread();
        praiseTread.setUserId(userId).setTypeId(typeId).setType("am");
        return praiseTreadService.estimate(praiseTread);
    }

    // 熔断方法
    public DataResult estimateGet(@PathVariable Integer userId,@PathVariable Integer typeId){
        DataResult result = new DataResult();
        result.setCode(500).setMsg("请求超时，请稍后重试~");
        return result;
    }


}
